<template>
  <div id="index">
    <el-form
      :inline="false"
      label-width="110px"
      label-position="right"
      :model="formAdd"
      ref="formAdd"
      :rules="rules"
    >
      <el-form-item label="todo title:" prop="title">
        <el-input size="mini" placeholder="请输入todo内容" v-model="formAdd.title" clearable></el-input>
      </el-form-item>
      <el-form-item label="todo content:" prop="content">
        <el-input
          type="textarea"
          size="mini"
          placeholder="请输入todo内容"
          v-model="formAdd.content"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="todo level:" prop="level">
        <el-slider
          :class="'level'+(formAdd.level)"
          :format-tooltip="formatTooltip"
          :max="5"
          v-model="formAdd.level"
        ></el-slider>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="addTodo('formAdd')">update</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: ["formAdd"],
  data() {
    var validateLevel = (rule, value, callback) => {
      if (value === 0) {
        callback(new Error("选择优先级"));
      } else {
        callback();
      }
    };
    return {
      rules: {
        title: [{ required: true, message: "请输入todo标题", trigger: "blur" }],
        content: [
          { required: true, message: "请输入todo内容", trigger: "blur" }
        ],
        level: [{ validator: validateLevel, trigger: "change" }]
      },
      tagTexts: ["", "不紧急", "稍微紧急", "一般紧急", "紧急", "非常紧急"]
    };
  },
  methods: {
    addTodo(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.formAdd.date = new Date().toLocaleString();
          let d = [];
          if (localStorage.getItem("todo") !== "") {
            d = JSON.parse(localStorage.getItem("todo"));
          }
          for (let i = 0; i < d.length; i++) {
            if (d[i].id === this.formAdd.id) {
              d[i] = this.formAdd;
            }
          }
          localStorage.setItem("todo", JSON.stringify(d));
          this.$emit("closeit", false);
          this.resetForm(formName);
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    formatTooltip(val) {
      return this.tagTexts[val];
    }
  }
};
</script>
<style scop>
#index .add-area {
  /* width: 300px; */
  margin: 0 20px 0 auto;
}
/* slider bar */
#index .level1 .el-slider__runway .el-slider__bar {
  background: #909399 !important;
  transition: 0.2s;
}
#index .level2 .el-slider__runway .el-slider__bar {
  background: #67c23a !important;
  transition: 0.2s;
}
#index .level3 .el-slider__runway .el-slider__bar {
  background: #409eff !important;
  transition: 0.2s;
}
#index .level4 .el-slider__runway .el-slider__bar {
  background: #e6a23c !important;
  transition: 0.2s;
}
#index .level5 .el-slider__runway .el-slider__bar {
  background: red !important;
  transition: 0.2s;
}
/* slider bar */
/* slider button */
#index .level1 .el-slider__runway .el-slider__button {
  border-color: #909399 !important;
}
#index .level2 .el-slider__runway .el-slider__button {
  border-color: #67c23a !important;
}
#index .level3 .el-slider__runway .el-slider__button {
  border-color: #409eff !important;
}
#index .level4 .el-slider__runway .el-slider__button {
  border-color: #e6a23c !important;
}
#index .level5 .el-slider__runway .el-slider__button {
  border-color: red !important;
}
/* slider button */
#index .el-rate i {
  font-size: 30px !important;
  margin-right: 20px !important;
  -webkit-transition: 0.3s;
  transition: 0.3s;
}
</style>
